﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.UI.Controls
{
    /// <summary>
    /// 用于类库之间传递类型的
    /// </summary>
    public sealed class TypePool
    {
        private TypePool()
        {
            pool = new Dictionary<string, object>();
        }

        private Dictionary<string, object> pool;
        private static TypePool instance;
        private static volatile object syncLock = new object();

        /// <summary>
        /// 唯一一实例
        /// </summary>
        internal static TypePool Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncLock)
                    {
                        if (instance == null)
                        {
                            instance = new TypePool();
                        }
                    }
                }

                return instance;
            }
        }

        /// <summary>
        /// 根据名称获取类型对应的类型字符串
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetTypeName(string name)
        {
            if (Instance.pool.ContainsKey(name))
            {
                object type = Instance.pool[name];
                if (type is Type)
                {
                    return type.ToString();
                }
                else
                {
                    return (string)type;
                }
            }

            return null;
        }

        /// <summary>
        /// 根据名称获取类型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Type GetType(string name)
        {
            if (Instance.pool.ContainsKey(name))
            {
                object type = Instance.pool[name];
                if (type is Type)
                {
                    return (Type)type;
                }
                else
                {
                    return Type.GetType((string)type);
                }
            }

            return null;
        }

        /// <summary>
        /// 根据索引名创建对应类型的类型实例。内部只使用了基本的 Activator.CreateInstance 调用，因此需要类型具有public且不带参数的构造函数。
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static object CreateInstance(string name)
        {
            Type type = GetType(name);
            if (type != null)
            {
                return Activator.CreateInstance(type);
            }

            return null;
        }

        /// <summary>
        /// 设置类型
        /// </summary>
        /// <param name="name"></param>
        /// <param name="typeString"></param>
        public static void SetType(string name, string typeString)
        {
            Instance.pool[name] = typeString;
        }

        /// <summary>
        /// 设置类型
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        public static void SetType(string name, Type type)
        {
            Instance.pool[name] = type;
        }
    }
}
